-- 装备系统测试和修复
-- 这个文件用于诊断和修复右键装备问题

-- 在游戏中按F1打开控制台，然后粘贴并运行以下代码：

local function TestEquipmentSystem()
    local player = ThePlayer
    if not player then
        print("错误：未找到玩家")
        return
    end
    
    print("=== 装备系统诊断 ===")
    
    -- 检查基本组件
    print("inventory 组件:", player.components.inventory ~= nil)
    print("playeractionpicker 组件:", player.components.playeractionpicker ~= nil)
    print("equippable 相关检查:", player.components.inventory.GetEquippedItem ~= nil)
    
    -- 检查是否有阻止动作的标签
    local blocking_tags = {"busy", "sleeping", "playerghost", "pono_speed_mode"}
    for _, tag in ipairs(blocking_tags) do
        if player:HasTag(tag) then
            print("警告 - 发现可能阻止动作的标签:", tag)
        end
    end
    
    -- 检查右键动作系统
    if player.components.playeractionpicker then
        print("playeractionpicker 组件存在")
        
        -- 尝试获取一个装备物品进行测试
        local inventory = player.components.inventory
        if inventory then
            for i = 1, inventory.maxslots do
                local item = inventory:GetItemInSlot(i)
                if item and item.components.equippable then
                    print("找到可装备物品:", item.prefab)
                    
                    -- 测试右键动作
                    local actions = player.components.playeractionpicker:GetRightClickActions(Vector3(0,0,0), item)
                    print("该物品的右键动作数量:", actions and #actions or "无")
                    
                    if actions and #actions > 0 then
                        for j, action in ipairs(actions) do
                            print("  动作", j, ":", action.action and action.action.id or "未知")
                        end
                    end
                    break
                end
            end
        end
    end
    
    print("=== 诊断结束 ===")
end

-- 尝试修复右键装备问题
local function FixEquipmentIssue()
    local player = ThePlayer
    if not player or not player.components.playeractionpicker then
        print("无法修复：缺少必要组件")
        return
    end
    
    print("尝试修复装备系统...")
    
    -- 确保右键装备动作存在
    if ACTIONS and ACTIONS.EQUIP then
        print("EQUIP 动作存在")
        
        -- 检查并修复 GetRightClickActions
        local old_GetRightClickActions = player.components.playeractionpicker.GetRightClickActions
        player.components.playeractionpicker.GetRightClickActions = function(self, pos, target, spellbook)
            local actions = old_GetRightClickActions(self, pos, target, spellbook)
            
            -- 如果目标是可装备物品且在物品栏中，添加装备动作
            if target and target.components.equippable and target.components.inventoryitem then
                local owner = target.components.inventoryitem.owner
                if owner == player then
                    -- 确保装备动作存在于动作列表中
                    local has_equip = false
                    for _, action in ipairs(actions) do
                        if action.action == ACTIONS.EQUIP then
                            has_equip = true
                            break
                        end
                    end
                    
                    if not has_equip and target.components.equippable:CanEquip(player) then
                        local equip_action = BufferedAction(player, target, ACTIONS.EQUIP)
                        table.insert(actions, equip_action)
                        print("添加了装备动作到:", target.prefab)
                    end
                end
            end
            
            return actions
        end
        
        print("装备系统修复完成")
    else
        print("错误：EQUIP 动作不存在")
    end
end

-- 运行诊断
TestEquipmentSystem()

-- 如果需要修复，运行以下命令：
-- FixEquipmentIssue()